<?php

/*
 * The server functions return an error JSON string in case of happening any error.
 * The error JSON string contains the JSON_ERROR_CODE and JSON_ERROR_MSG fields.
 * To check whether during the execution of any function occured any error the isError() function can be used.
 */

///////////////////////////////////////////////////////////
// error JSON string keys
//

define('JSON_ERROR_CODE', "ErrorCode");
define('JSON_ERROR_MSG', "ErrorMsg");

///////////////////////////////////////////////////////////
// ERROR CODES
//

//
// General errors starting from 0
//

/**
 * General (unspecified/unknown) error
 */
define('ERROR_GENERAL', 0);

/**
 * Missing a mandatory argument, so can't execute the requested procedure.
 */
define('ERROR_MISSING_ARGUMENT', 1);

//
// Update location errors starting from 100
//

/**
 * If the lon or lat value is out of valid range: -180<lon<180 and -90<lat<90
 */
define('ERROR_UPDATELOCATION_LONLAT_INVALID', 101);

/**
 * Missing permission for posting to the FB wall stream.
 */
define('ERROR_UPDATELOCATION_MISSING_POST_PERMISSION', 102);

/**
 * The user denied to allow permission to publish to FB wall.
 */
define('ERROR_UPDATELOCATION_DENIED_POST_PERMISSION', 103);

/**
 * If the lon or lat value is missing.
 */
define('ERROR_UPDATELOCATION_LONLAT_MISSING', 104);


//
// get Locations erros starting from 200
//

define('ERROR_GETLOCATION_WRONG_EXTRA_FIELD', 201);


//
// Geolocation errors starting from 500
//

/**
 * If can't connect to the geolocation server.
 */
define('ERROR_GEOLOCATION_SERVER_UNAVAILABLE', 501);

/**
 * If the geolocation server returned non-SUCESS (200) response code in its response data
 */
define('ERROR_GEOLOCATION_REQUEST_FAIL', 502);

/**
 * If the lon or lat value is out of valid range: -180<lon<180 and -90<lat<90
 */
define('ERROR_GEOLOCATION_LONLAT_INVALID', 503);


//
// Session related errors starting from 600
//

/**
 * If the session data is invalid or not found.
 */
define('ERROR_SESSION_INVALID_OR_MISSING', 601);




//
// Functions
//

/**
 * Creates a JSON string representing the description of an error.
 *
 * @param Integer $error_code error code
 * @param String $error_msg error message
 * @return JSON string representing the given error
 */
function getErrorAsJson($error_code, $error_msg) {
    $resultArray = array();
    $resultArray[JSON_ERROR_CODE] = $error_code;
    $resultArray[JSON_ERROR_MSG] = $error_msg;
    return json_encode($resultArray);
}

/**
 * Creates a JSON string representing the given exception.
 * 
 * @param Exception $e the Exception object
 * @return string JSON-encoded error string
 */
function getExceptionAsJson($e) {
    $resultArray = array();
    $resultArray[JSON_ERROR_CODE] = $e->getCode();
    $resultArray[JSON_ERROR_MSG] = $e->getMessage();
    return json_encode($resultArray);
}

/**
 * Checks whether the given JSON string represents a Siture server error.
 * 
 * @param String $jsonString error JSON String or something else
 * @return Boolean true if the passed JSON string represents an error
 */
function isError($jsonString) {
    $jsonArray = json_decode($jsonString, TRUE);
    //TODOLogger::getLogger()->debug("isError(): jsonArray = " . Logger::arrayToString($jsonArray));

    return ($jsonArray!=NULL && is_array($jsonArray) && count($jsonArray)==2
            && isset($jsonArray[JSON_ERROR_CODE]) && isset($jsonArray[JSON_ERROR_MSG]));
}

/**
 * Returns the message part of the JSON error string.
 *
 * @param String $errorJson error JSON string
 * @return String the message part of the JSON error string
 */
function getErrorMessage($errorJson) {
    $errorArray = json_decode($errorJson, TRUE);

    if (is_array($errorArray) && isset ($errorArray[JSON_ERROR_MSG])) {
        return $errorArray[JSON_ERROR_MSG];
    }

    return NULL;
}

?>
